<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene" />
    <el-card style="margin: 10px 0px;">
      <!-- v-if|v-show都可以实现显示与隐藏 但if会销毁组件 -->
      <div v-show="scene == 0">
        <el-button @click="addSpu" type="primary" size="default" icon="Plus"
          :disabled="categoryStore.c3Id ? false : true">添加SPU</el-button>
        <el-table style="margin: 10px 0px;" border :data="records">
          <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row, $index }">
              <el-button type="primary" size="small" icon="Plus" title="添加SKU" @click="addSku(row)"></el-button>
              <el-button type="primary" size="small" icon="Edit" title="修改SPU" @click="updateSpu(row)"></el-button>
              <el-button type="primary" size="small" icon="View" title="查看SKU列表" @click="findSku(row)"></el-button>
              <el-popconfirm :title="`你确定要删除${row.spuName}吗?`" width="200px" @confirm="deleteSpu(row)">
                <template #reference>
                  <el-button type="danger" size="small" icon="Delete" title="删除SPU列表"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="pagNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
          :background="true" layout="prev, pager, next, jumper,->, sizes,total" :total="total"
          @current-change="getHasSpu" @size-change="changesize" />
      </div>
      <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"></SpuForm>
      <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene"></SkuForm>
      <!-- 查看SKU列表-dialog对话框 -->
      <el-dialog title="SkU列表" v-model="show">
        <el-table border :data="skuArr">
          <el-table-column prop="skuName" label="SKU名字"></el-table-column>
          <el-table-column prop="price" label="SKU价格"></el-table-column>
          <el-table-column prop="weight" label="SKU重量"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img :src="row.skuDefaultImg" style="width: 100px; height: 100px;">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>
<script setup lang='ts' name='spu'>
import { onBeforeUnmount, ref, watch } from 'vue';
import { reqHasSpu, reqRemoveSpu, reqSkuList } from '@/api/product/spu';
import type { HasSpuResponseData, Records, SkuData, SpuData } from '@/api/product/spu/type';
import useCategoryStore from '@/store/modules/category'
//引入子组件
import SpuForm from './spuForm.vue';
import SkuForm from './skuForm.vue';
import { ElMessage } from 'element-plus';
let categoryStore = useCategoryStore()
//切换场景
let scene = ref<number>(0)   //0显示已有SPU 1：添加或者修改已有SPU  2：添加SKU
//分页器默认页码
let pagNo = ref<number>(1)
//分页器一页展示几个
let pageSize = ref<number>(3)
//存储已有SPU数据
let records = ref<Records>([])
//存储总SPU个数
let total = ref<number>(0)
//获取子组件实例
let spu = ref<any>()
let sku = ref<any>()
//存储SKU数组
let skuArr = ref<SkuData[]>([])
//查看SKU列表dialog对话框
let show = ref<boolean>(false)


//监听三级id发请求
watch(() => categoryStore.c3Id, () => {
  if (!categoryStore.c3Id) return
  getHasSpu()
})
//获取三级分类下的已有的SPU  pager@current-change paginatio组件会自动传入
const getHasSpu = async (pager = 1) => {
  pagNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(pagNo.value, pageSize.value, categoryStore.c3Id)
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//下拉菜单页码改变
const changesize = () => {
  getHasSpu()
}

//添加SPU按钮回调 
const addSpu = () => {
  scene.value = 1
  spu.value.initAddSpu(categoryStore.c3Id)
}
//修改SPU按钮回调
const updateSpu = (row: SpuData) => {
  scene.value = 1
  //调用子组件实例方法获取完整已有的spu数据  spu为子组件ref获取的子组件实例
  spu.value.initHasSpuData(row)
}
//子组件SpuFrom绑定自定义事件：让子组件通知父组件场景切换为0
const changeScene = (Obj: any) => {
  scene.value = Obj.flag
  //更新或修改成功后再次获取已有SPU
  if (Obj.params == 'update') {
    // 更新留在当前页
    getHasSpu(pagNo.value)
  } else {
    // 添加留在第一页
    getHasSpu()
  }

}

//添加SKU按钮回调
const addSku = (row: SpuData) => {
  //切换场景
  scene.value = 2
  // 调用子组件实例
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
//查看SKU列表按钮
const findSku = async (row: SpuData) => {
  let result = await reqSkuList((row.id as number))
  if (result.code == 200) {
    skuArr.value = result.data
    //对话框显示出来
    show.value = true
  }
}
//删除SPU按钮回调
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu((row.id as number))
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: "删除SPU成功"
    })
    //获取剩余已有SPU数组
    getHasSpu(records.value.length > 1 ? pagNo.value : pagNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: "删除SPU失败"
    })
  }
}
onBeforeUnmount(()=>{
  categoryStore.$reset()
})
</script>


<style scoped></style>